﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using WP_GasTracker.Models;

namespace WP_GasTracker.Persitence
{
    public class IsolatedStorageGateway
    {
        private const string StorageFile = "data.txt";
        private readonly XmlSerializer _serializer;

        public IsolatedStorageGateway()
        {
            _serializer = new XmlSerializer(typeof(Notebook));
        }

        public Notebook LoadNotebook()
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = GetStorageStreamForReading(store))
                using (var reader = new StreamReader(stream))
                {
                    return reader.EndOfStream
                        ? new Notebook()
                        : (Notebook)_serializer.Deserialize(reader);
                }
            }
        }

        public NotebookEntry LoadEntry(Guid guid)
        {
            var notebook = LoadNotebook();
            return notebook.Where(x => x.Id == guid).FirstOrDefault();
        }

        public void StoreEntry(NotebookEntry entry)
        {
            var notebook = LoadNotebook();
            AssignId(entry);
            RemoveExistingEntryFromNotebook(notebook, entry);
            notebook.Add(entry);
            WriteNotebookToStorage(notebook);
        }

        public void DeleteEntry(NotebookEntry entry)
        {
            var notebook = LoadNotebook();
            RemoveExistingEntryFromNotebook(notebook, entry);
            WriteNotebookToStorage(notebook);
        }

        private void WriteNotebookToStorage(Notebook notebook)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            using (var stream = GetStorageStreamForWriting(store))
            {
                _serializer.Serialize(stream, notebook);
            }
        }

        private static void AssignId(NotebookEntry entry)
        {
            if (entry.Id == Guid.Empty) entry.Id = Guid.NewGuid();
        }

        private static void RemoveExistingEntryFromNotebook(Notebook notebook, NotebookEntry entry)
        {
            var toRemove = notebook.Where(x => x.Id == entry.Id).FirstOrDefault();
            if (toRemove == null) return;
            notebook.Remove(toRemove);
        }

        private static IsolatedStorageFileStream GetStorageStreamForWriting(IsolatedStorageFile store)
        {
            return new IsolatedStorageFileStream(StorageFile, FileMode.Create, FileAccess.Write, store);
        }

        private static IsolatedStorageFileStream GetStorageStreamForReading(IsolatedStorageFile store)
        {
            return new IsolatedStorageFileStream(StorageFile, FileMode.OpenOrCreate, FileAccess.Read, store);
        }
    }
}
